package com.yuedou.api.service.platfom.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuedou.api.dao.mer.MerMapper;
import com.yuedou.api.dao.mer.MerUserMapper;
import com.yuedou.api.dao.platform.SplitAccountMapper;
import com.yuedou.api.dao.wxuser.UserMapper;
import com.yuedou.api.entity.mer.Mer;
import com.yuedou.api.entity.mer.MerUser;
import com.yuedou.api.entity.platform.SplitAccount;
import com.yuedou.api.entity.wxuser.User;
import com.yuedou.api.service.base.impl.BaseServiceImpl;
import com.yuedou.api.service.mer.MerUserService;
import com.yuedou.api.service.platfom.SplitAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分成账户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-08
 */
@Service
public class SplitAccountServiceImpl extends BaseServiceImpl<SplitAccountMapper, SplitAccount> implements SplitAccountService {

    @Resource
    private SplitAccountMapper splitAccountMapper;
    @Resource
    private MerUserMapper merUserMapper;
    @Resource
    private MerMapper merMapper;
    @Resource
    private MerUserService merUserService;
    @Resource
    private UserMapper userMapper;

    /**
     * 账户类型  0 - 平台   1 - 物业  2 -  小区  3 - 商家   4 - 个人
     * @param userId
     * @param accountType
     * @return
     */
    @Override
    public SplitAccount getAccountByUserId(int userId, int accountType, int accountId) {
        QueryWrapper<SplitAccount> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("account_type",accountType);
        qw.eq("account_id",accountId);
        qw.last("limit 1");
        return splitAccountMapper.selectOne(qw);
    }

    /**
     * 账户类型  0 - 平台   1 - 物业  2 -  小区  3 - 商家   4 - 个人
     * @param merId
     * @return
     */
    @Override
    public SplitAccount getAdminAccountByType(int merId,int accountType) {
        QueryWrapper<MerUser> qwMerUser = new QueryWrapper<>();
        qwMerUser.eq("mer_id",merId);
        qwMerUser.eq("is_admin",1);
        qwMerUser.last("limit 1");
        MerUser merUser = merUserMapper.selectOne(qwMerUser);
        if(merUser != null){
            QueryWrapper<SplitAccount> qw = new QueryWrapper<>();
            qw.eq("user_id", merUser.getUserId());
            qw.eq("account_id", merId);
            qw.eq("account_type",accountType);
            qw.last("limit 1");
            return splitAccountMapper.selectOne(qw);
        }
        return null;
    }

    @Override
    public List<SplitAccount> getAccountByUserId(int userId,int is_person) {
        QueryWrapper<SplitAccount> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        if(is_person == 1){
            qw.eq("account_type",4);
        }else{
            qw.ne("account_type",4);
        }
        List<SplitAccount> list = splitAccountMapper.selectList(qw);
        for (SplitAccount splitAccount : list){
            splitAccount.setIsAdmin(0);
            if(splitAccount.getAccountType() == 0){//平台

            }if(splitAccount.getAccountType() == 3){//商家
                Mer mer = merMapper.selectById(splitAccount.getAccountId());
                if(mer!=null){
                    splitAccount.setAccountName(mer.getShopName());

                    MerUser merUser = merUserService.getMerByUser(userId,splitAccount.getAccountId());
                    if(merUser!=null){
                        splitAccount.setIsAdmin(merUser.getIsAdmin());
                    }

                }
            } else if(splitAccount.getAccountType() == 4){//个人
                User user = userMapper.selectById(splitAccount.getAccountId());
                if(user!=null){
                    splitAccount.setAccountName(user.getNickName());
                }
            }
        }
        return list;
    }
}
